package com.kexio.common.exception;

/**
 * 业务异常类
 * 
 * 用于处理业务逻辑相关的异常情况
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class BusinessException extends BaseException {
    
    private static final long serialVersionUID = 1L;
    
    /** 默认业务异常错误码 */
    public static final int DEFAULT_ERROR_CODE = 400;
    
    // ==================== 构造函数 ====================
    
    public BusinessException() {
        super();
        setErrorCode(DEFAULT_ERROR_CODE);
    }
    
    public BusinessException(String message) {
        super(message);
        setErrorCode(DEFAULT_ERROR_CODE);
    }
    
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        setErrorCode(DEFAULT_ERROR_CODE);
    }
    
    public BusinessException(Throwable cause) {
        super(cause);
        setErrorCode(DEFAULT_ERROR_CODE);
    }
    
    public BusinessException(int errorCode, String message) {
        super(errorCode, message);
    }
    
    public BusinessException(int errorCode, String message, Throwable cause) {
        super(errorCode, message, cause);
    }
    
    public BusinessException(int errorCode, String message, Object errorDetails) {
        super(errorCode, message, errorDetails);
    }
    
    public BusinessException(int errorCode, String message, Object errorDetails, Throwable cause) {
        super(errorCode, message, errorDetails, cause);
    }
    
    // ==================== 静态工厂方法 ====================
    
    /**
     * 创建业务异常
     * 
     * @param message 错误消息
     * @return BusinessException实例
     */
    public static BusinessException of(String message) {
        return new BusinessException(message);
    }
    
    /**
     * 创建业务异常
     * 
     * @param errorCode 错误码
     * @param message 错误消息
     * @return BusinessException实例
     */
    public static BusinessException of(int errorCode, String message) {
        return new BusinessException(errorCode, message);
    }
    
    /**
     * 创建业务异常
     * 
     * @param message 错误消息
     * @param cause 原因异常
     * @return BusinessException实例
     */
    public static BusinessException of(String message, Throwable cause) {
        return new BusinessException(message, cause);
    }
    
    /**
     * 创建业务异常
     * 
     * @param errorCode 错误码
     * @param message 错误消息
     * @param cause 原因异常
     * @return BusinessException实例
     */
    public static BusinessException of(int errorCode, String message, Throwable cause) {
        return new BusinessException(errorCode, message, cause);
    }
    
    // ==================== 常用业务异常 ====================
    
    /**
     * 参数无效异常
     * 
     * @param paramName 参数名
     * @return BusinessException实例
     */
    public static BusinessException invalidParam(String paramName) {
        return new BusinessException(400, "参数无效: " + paramName);
    }
    
    /**
     * 参数无效异常
     * 
     * @param paramName 参数名
     * @param paramValue 参数值
     * @return BusinessException实例
     */
    public static BusinessException invalidParam(String paramName, Object paramValue) {
        return new BusinessException(400, "参数无效: " + paramName + " = " + paramValue);
    }
    
    /**
     * 资源不存在异常
     * 
     * @param resourceName 资源名称
     * @return BusinessException实例
     */
    public static BusinessException resourceNotFound(String resourceName) {
        return new BusinessException(404, resourceName + "不存在");
    }
    
    /**
     * 资源不存在异常
     * 
     * @param resourceName 资源名称
     * @param resourceId 资源ID
     * @return BusinessException实例
     */
    public static BusinessException resourceNotFound(String resourceName, Object resourceId) {
        return new BusinessException(404, resourceName + "不存在: " + resourceId);
    }
    
    /**
     * 操作不被允许异常
     * 
     * @param operation 操作名称
     * @return BusinessException实例
     */
    public static BusinessException operationNotAllowed(String operation) {
        return new BusinessException(403, "操作不被允许: " + operation);
    }
    
    /**
     * 状态异常
     * 
     * @param expectedStatus 期望状态
     * @param actualStatus 实际状态
     * @return BusinessException实例
     */
    public static BusinessException invalidStatus(String expectedStatus, String actualStatus) {
        return new BusinessException(400, "状态异常，期望: " + expectedStatus + "，实际: " + actualStatus);
    }
    
    /**
     * 数据已存在异常
     * 
     * @param dataName 数据名称
     * @return BusinessException实例
     */
    public static BusinessException dataExists(String dataName) {
        return new BusinessException(409, dataName + "已存在");
    }
    
    /**
     * 数据已存在异常
     * 
     * @param dataName 数据名称
     * @param dataValue 数据值
     * @return BusinessException实例
     */
    public static BusinessException dataExists(String dataName, Object dataValue) {
        return new BusinessException(409, dataName + "已存在: " + dataValue);
    }
    
    /**
     * 认证失败异常
     * 
     * @param message 错误消息
     * @return BusinessException实例
     */
    public static BusinessException authFailed(String message) {
        return new BusinessException(401, "认证失败: " + message);
    }
    
    /**
     * 操作失败异常
     * 
     * @param message 错误消息
     * @return BusinessException实例
     */
    public static BusinessException operationFailed(String message) {
        return new BusinessException(500, "操作失败: " + message);
    }
    
    /**
     * 权限不足异常
     * 
     * @param operation 操作名称
     * @return BusinessException实例
     */
    public static BusinessException accessDenied(String operation) {
        return new BusinessException(403, "权限不足: " + operation);
    }
    
    /**
     * 系统异常
     * 
     * @param message 错误消息
     * @return BusinessException实例
     */
    public static BusinessException systemError(String message) {
        return new BusinessException(500, "系统异常: " + message);
    }
}
